const http = require("http"), fs = require("fs"), querystring = require("querystring"), cylist = require("./cy.json");
let index = 7, nj = 0;
getUrl();

function getUrl(cy){
    index++;
    if(nj == 0 && index >= cylist.list.length){
        index = 0;
        nj++;
    }else if(nj > 0 && index >= cylist.sj[nj].length){
        index = 0;
        nj++;
        if(!(nj in cylist.sj)){
            fs.writeFile("cy2.json", JSON.stringify(cylist, 1), ()=>{});
            return;
        }
    }

    let postData = querystring.stringify({
        "lb_a":"hp",
        "lb_b":"mh",
        "lb_c":"mh",
        "tp":"tp1",
        "q":nj?cylist.sj[nj][index][0]:cylist.list[index][0]
    });
    let options = {
        hostname: 'www.zdic.net',
        port: 80,
        path: '/sousuo/',
        method: 'POST',
        headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(postData)
        }
    };
    
    let req = http.request(options, (res) => {
        console.log(`STATUS: ${res.statusCode}`);
        //console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
        if(res.statusCode == 302){
            http.get("http://www.zdic.net" + res.headers['location'], (rs)=>{
                rs.setEncoding('utf8');
                let rawData = '';
                rs.on('data', (chunk) => { rawData += chunk; });
                rs.on('end', () => {
                    let bp = rawData.indexOf("align=\"absmiddle\">"), ep = 0;
                    bp = rawData.indexOf("align=\"absmiddle\">", bp + 1);
                    ep = rawData.indexOf("<", bp);
                    console.log("py", bp, ep);
                    let des = rawData.substr(bp + 18, ep - bp - 18) + "\n";
                    for(let i=0;i<10;i++){
                        bp = rawData.indexOf('">◎</i>', bp);
                        if(bp == -1) break;
                        ep = rawData.indexOf('</p>', bp);
                        des += rawData.substr(bp + 7, ep - bp - 7) + "\n";
                        bp = ep;
                    }
                    console.log(des);
                    if(nj)
                        cylist.sj[nj][index][1] = des;
                    else
                        cylist.list[index][1] = des;
                    getUrl();
                    //fs.writeFile("cy2.json", JSON.stringify(cylist), ()=>{});
                });
            });
        }else{
            index++;
            getUrl();
        }
        let cd = Buffer.allocUnsafe(0);
        //res.setEncoding('utf8');
        res.on('data', (chunk) => {
            //console.log(typeof(chunk));
            cd = Buffer.concat([cd, chunk], cd.length + chunk.length);
        });
        res.on('end', () => {
            //console.log('end', cd.toString());
        });
    });
    
    req.on('error', (e) => {
        console.error(`problem with request: ${e.message}`);
    });
    
    // write data to request body
    req.write(postData);
    req.end();
}
  
  